package com.cloud.pole.user.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.user.model.vo.account.UserWithdrawParamsVo;
import com.cloud.pole.user.service.account.UserWithdrawService;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("userWithdraw/front")
@Slf4j
public class UserWithdrawController{

	@Autowired
	UserWithdrawService userWithdrawService;
	
	@ResponseBody
    @RequestMapping(value = "/addUserWithdraw")
    public ApiRestResponse<Boolean> addUserWithdraw(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String accountId,
    		@RequestParam(defaultValue = "") String bankTypeId,
    		@RequestParam(defaultValue = "") String bankName,
    		@RequestParam(defaultValue = "") String userRealName,
    		@RequestParam(defaultValue = "") String userCardNo,
    		@RequestParam(defaultValue = "") String amount,
    		@RequestParam(defaultValue = "") String payPwd){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			ParamException.err(StringUtils.isBlank(accountId), "accountId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(accountId), "accountId错误");
			ParamException.err(StringUtils.isBlank(bankTypeId), "bankTypeId不能为空");
			ParamException.err(StringUtils.isBlank(bankName), "bankName不能为空");
			ParamException.err(StringUtils.isBlank(userRealName), "userRealName不能为空");
			ParamException.err(StringUtils.isBlank(userCardNo), "userCardNo不能为空");
			ParamException.err(StringUtils.isBlank(payPwd), "payPwd不能为空");
			ParamException.err(StringUtils.isBlank(amount), "amount不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(amount), "amount错误");
			
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userWithdrawService.addUserWithdraw(token, Long.parseLong(accountId), bankTypeId, bankName, userRealName, userCardNo, Integer.parseInt(amount), payPwd));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
    @RequestMapping(value = "/getUserWithdrawParams")
    public ApiRestResponse<UserWithdrawParamsVo> getUserWithdrawParams(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String accountId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			ParamException.err(StringUtils.isBlank(accountId), "accountId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(accountId), "accountId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userWithdrawService.getUserWithdrawParams(token, Long.parseLong(accountId)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
